import {
	defineStore
} from 'pinia'
import {
	ref,
	computed
} from 'vue'
import piniaPersistConfig from "@/utils/persist"
import {
	dataSerialize
} from "@/utils/md5/index.js"
import {
	useAppStore
} from "@/store/app.js"

import {
	isWeChatBrowser,
} from '@/common/common'

export const useUserStore = defineStore('user', () => {
	const appStore = useAppStore()
	
	// 今日展示公告弹窗
	const showNoticeToday = ref(true)
	const showNoticeDate = ref('')
	function getNoticeDate(){
		const now = new Date();
		const year = now.getFullYear();
		const month = now.getMonth() + 1; // 月份从0开始，需要加1
		const day = now.getDate();
		console.log(`${year}-${month}-${day}`);
		return `${year}-${month}-${day}`
	}
	
	const free_time = ref(0)
	const free_timmer = ref(null)
	
	const pay_time = ref(0)
	const is_vip = ref(false)
	const is_recharge = ref(false)
	
	const default_header = ''
	const autoLogin = ref(true)
	const user_id = ref('')
	const login_code = ref('')
	const open_id = ref('')
	const user_info = ref({
		head_image:default_header,
		token:''
	}) 
	const login_channel = ref('') // 登录渠道
	const isLogin = computed(() => {
		// #ifdef MP-WEIXIN
		if(user_info.value.token){
			return true
		}else{
			return false
		}
		// #endif
		
		// #ifdef H5
		if(user_info.value.token){
			return true
		}else{
			return false
		}
		
		// #endif
		
		// #ifdef APP
		if(user_info.value.token){
			return true
		}else{
			return false
		}
		// #endif
		
		// #ifdef MP-TOUTIAO
		if(user_info.value.token){
			return true
		}else{
			return false
		}
		// #endif
	})
	
	const exitLogin = ()=>{
		appStore.redirect = ''
		showNoticeToday.value = true
		showNoticeDate.value = ''
		
		autoLogin.value = false
		user_id.value = ''
		login_code.value = ''
		open_id.value = ''
		user_info.value = {
			head_image:default_header,
			token:''
		}
		login_channel.value = ''
	}
	
	// return 仓库数据，外部调用
	return {
		free_time,
		free_timmer,
		pay_time,
		is_vip,
		is_recharge,
		
		autoLogin,
		user_id,
		login_code,
		open_id,
		user_info,
		isLogin,
		exitLogin,
		login_channel,
		
		showNoticeToday,
		showNoticeDate,
		getNoticeDate,
	}
}, {
	persist: piniaPersistConfig('userStore')
})